<?php
require_once('start_session.php');

// header
require_once("header.php");
require_once('db_connect.php');
require_once('global_vars.php');
//require_once('search_bar.php');
require_once('connect_info.php');
//require_once('survey_list.php');
if (isset($_COOKIE['user_id']) && $_COOKIE['user_id'] != ADMIN_ID)
	require_once('member_info.php');

	/*
echo "cookie : ".$_COOKIE['user_id']."<br >";
echo "s_ip : ".$_SESSION['ip']."<br >";
echo "s_con_time : ".$_SESSION['connect_time']."<br >";
echo "s_location :".$_SESSION['location']."<br >";
	*/
?>
<div id="viewresult_main_div">
<?php

	$getv_sid = $_GET["sid"];
	$getv_select_question = $_GET["select_question"];
	$getv_select_item = $_GET["select_item"];
	$getv_select_condition = $_GET["select_condition"];
	$getv_select_date = $_GET["select_date"];
	$getv_lower_time = $_GET["lower_time"];
	$getv_upper_time = $_GET["upper_time"];
	
	//default
	if(!isset($getv_select_question)) $getv_select_question = 0;
	if(!isset($getv_select_item)) $getv_select_item = 0;
	if(!isset($getv_select_condition)) $getv_select_condition = 0;
	if(!isset($getv_select_date)) $getv_select_date = 0;
	if(!isset($getv_lower_time)) $getv_lower_time = '00';
	if(!isset($getv_upper_time)) $getv_upper_time = '24';

	/*
	echo "-------------------------------------<br>";
	echo "getv_sid = $getv_sid<br>";
	echo "getv_select_question = $getv_select_question<br>";
	echo "getv_select_item = $getv_select_item<br>";
	echo "getv_select_condition = $getv_select_condition<br>";
	echo "getv_select_date = $getv_select_date<br>";
	echo "getv_lower_time = $getv_lower_time<br>";
	echo "getv_upper_time = $getv_upper_time<br>";
	echo "-------------------------------------<br><br>";
	*/
	$sql = "select * from survey where id = $getv_sid";
	
	$result = mysqli_query($conn, $sql);
	$num_row = mysqli_num_rows($result);
	if($num_row != 1)
	{
		//die("sid 무효. 종료");
		echo "sid 무효. 종료<br />";
	}
	
	//select cast(dt as time) from timetest where cast(dt as time) between '15:00:00' and '24:00:00';
	
	$survey_row = mysqli_fetch_array($result);
	/*
	$survey_row[id]
	$survey_row[title];
	$survey_row[is_once];
	$survey_row[start_date];
	$survey_row[end_date];
	$survey_row[start_time];
	$survey_row[end_time];
	$survey_row[max_capacity];
	$survey_row[require_login];
	$survey_row[ap_id];
	$survey_row[gps_id];
	$survey_row[category];
	$survey_row[member_id];
	$survey_row[view_count];
	*/
?>
	<h3>설문 결과</h3>
	<hr noshade>
	<div id="viewresult_main_div_title">
	<br>
	<font id="viewresult_main_font_head_title">설문 제목 : </font><font id="viewresult_main_font_head_contents_big"><?=$survey_row[title]?></font><br />
	<font id="viewresult_main_font_head_title">카테고리 : </font><font id="viewresult_main_font_head_contents_medium"><?=$survey_row[category]?></font><br />
	<font id="viewresult_main_font_head_title">관련 키워드 : </font><font id="viewresult_main_font_head_contents_medium">
	<?
	$result = mysqli_query($conn, "select * from keyword_survey where survey_id = '$survey_row[id]'");
	$keyword_survey_row = mysqli_fetch_array($result);
	echo "$keyword_survey_row[keyword]";
	while($keyword_survey_row = mysqli_fetch_array($result))
	{
		echo ",  $keyword_survey_row[keyword]";
	}
	?>
	</font><br />

	<font id="viewresult_main_font_head_title">설문 기간 : </font><font id="viewresult_main_font_head_contents_medium"><?=$survey_row[start_date]?> <font style='color:#0A6ECD;size:20px'>~</font> <?=$survey_row[end_date]?></font><br />
	<font id="viewresult_main_font_head_title">참여가능 시간대 : </font><font id="viewresult_main_font_head_contents_medium"><?=$survey_row[start_time]?> <font style='color:#0A6ECD;size:20px'>~</font> <?=$survey_row[end_time]?></font><br />
	</div>
	<br><hr noshade><br>
	
	<div id="viewresult_main_div_searchform">
	<form id="viewresult_form" method="get" action="survey_viewresult.php">
	<input type="hidden" name="sid" value="<?=$getv_sid?>">
    <font id="viewresult_main_font_searchform">문항 선택</font>&nbsp;
	<select name="select_question" onchange="changeSubject(this.form)">
	</select>
	<br />

	<font id="viewresult_main_font_searchform">항목 선택</font>&nbsp;
	<select name="select_item" onchange="changeContents(this.form)">
	</select>
	<br />
	
	<font id="viewresult_main_font_searchform">보기 기준</font>&nbsp;
	<select name="select_condition" style="width:160; font-size:17;" xonchange="selChange(this)">
        <option value="0" selected>선택 안함</option>
        <option value="1">남녀 응답비율</option>
        <option value="2">최종학력별 응답비율</option>
        <option value="3">연령대별 응답비율</option>
    </select>
	<br />
	
	<font id="viewresult_main_font_searchform">날짜</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<select name="select_date" style="width:160; font-size:17;" xonchange="selChange(this)">
        <option value="0" selected>전체 날짜</option>
<?php

$start = strtotime($survey_row[start_date]);
$end = strtotime($survey_row[end_date]);
for($i=$start,$v=1;$i<=$end;$i=$i+86400,$v++)
{
	echo "\t<option value=\"";
	echo date("Y-m-d", $i);
	echo "\">";
	echo date("Y-m-d", $i);
	echo "</option>\n";
}

?>
	</select>
	<br />

	<font id="viewresult_main_font_searchform">시간대</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type=text name=lower_time style='width:17px;' value='00'>시 ~ <input type=text name=upper_time style='width:17px' value='24'>시&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	
	<input type=submit value="보기" style='width:70px;height:25px' xonclick=view_submit()>
	<!--<img src=http://chart.apis.google.com/chart?chs=250x100&chd=t:60,30,10&cht=p3&chl=안녕|월드|헤헤&chco=FF00F0>-->
	
	</form><br>
	</div>

<?

//항목
$question_query = "select * from question where survey_id = '$getv_sid'";
if($getv_select_question != 0) //문항 조건 있는 경우 AND qnum = '보고싶은 문항번호'
{
	$question_query.= " and qnumber = '$getv_select_question'";
}


$question_query_result = mysqli_query($conn, $question_query);
$question_num = 0;

//날짜, 시간대 검색 조건쿼리(where) 생성
$date_query = '';
$time_query = '';



if($getv_select_date != 0)
{
	$date_query = " and submit_time like '%$getv_select_date%'";
	//echo $date_query;
}

//참고 : select cast(dt as time) from timetest where cast(dt as time) between '15:00:00' and '24:00:00';
$time_query = " and cast(submit_time as time) between '$getv_lower_time:00:00' and '$getv_upper_time:00:00';";
//echo "date_query : $date_query <br>";
//echo "time_query : $time_query <br>";

?>
<hr noshade>



<div id="viewresult_main_div_searchform">
<center>< 이 설문의 응답자 ></center>


<?

$n_answer = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid'"));

//남여 응답자수 비율
$n_man = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and gender = '1'"));
$n_woman = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and gender = '2'"));

//학력별 응답자수 비율
$n_edulevel1 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and edulevel = '1'"));
$n_edulevel2 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and edulevel = '2'"));
$n_edulevel3 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and edulevel = '3'"));
$n_edulevel4 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and edulevel = '4'"));
$n_edulevel5 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and edulevel = '5'"));

//연령대별 응답자수 비율
$n_age1 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and age = '1'"));
$n_age2 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and age = '2'"));
$n_age3 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and age = '3'"));
$n_age4 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and age = '4'"));
$n_age5 = mysqli_num_rows(mysqli_query($conn, "select * from answer where survey_id = '$getv_sid' and age = '5'"));
?>

<font id="viewresult_main_font_searchstat">총 응답자수 : </font><?=$n_answer?> 명<br />
<font id="viewresult_main_font_searchstat">성별 - </font>남성 : <?=$n_man?> 명 <b>/</b> 여성 : <?=$n_woman?> 명<br />
<font id="viewresult_main_font_searchstat">학력별 - </font>중졸 이하 : <?=$n_edulevel1?> 명 <b>/</b> 고졸 : <?=$n_edulevel2?> 명 <b>/</b> 초대졸 : <?=$n_edulevel3?> 명 <b>/</b> 대졸 : <?=$n_edulevel4?> 명 <b>/</b> 대학원졸 이상 : <?=$n_edulevel5?> 명<br />
<font id="viewresult_main_font_searchstat">연령별 - </font>10대 이하 : <?=$n_age1?> 명 <b>/</b> 20대 : <?=$n_age2?> 명 <b>/</b> 30대 : <?=$n_age3?> 명 <b>/</b> 40대 : <?=$n_age4?> 명 <b>/</b> 50대 이상 : <?=$n_age5?> 명<br />

</div>
<hr noshade>





<div id="viewresult_main_div_searchform">
<br />
<font id="viewresult_main_font_searchcond">대상 날짜 : </font><u>
<?if($getv_select_date == 0) echo "선택안함";
else echo $getv_select_date."";?></u>
<br />

<font id="viewresult_main_font_searchcond">대상 시간대 : </font><u><?=$getv_lower_time?> 시 ~ <?=$getv_upper_time?> 시</u>
<br />

<font id="viewresult_main_font_searchcond">보기 기준 : </font><u>
<?
if($getv_select_condition == 0)	echo "선택안함";
else if($getv_select_condition == 1) echo "남녀 응답비율";
else if($getv_select_condition == 2) echo "최종학력별 응답비율";
else if($getv_select_condition == 3) echo "연령대별 응답비율";
?></u>
<br /><br />
</div>
<hr noshade>


<div id="viewresult_main_div_contents">
<?
while($question_row = mysqli_fetch_array($question_query_result))
{
/*
	$question_row[survey_id]
	$question_row[qnumber]
	$question_row[qtitle]
	$question_row[is_multi]
*/
	$question_num++;
	
	echo "<br>";
	if($getv_select_question == 0)
	{
		echo "<font id=\"participate_main_font_question_num\">$question_num) </font>";
	}
	echo "<font id=\"viewresult_main_font_question\">$question_row[qtitle]</font><br><br>";

	$title_for_a_item = range(0,0);	//배열 초기화
	$count_for_a_item = range(0,0);
	
	$item_num = 0;
	
	$item_query = "select * from item where survey_id = '$getv_sid' and qnum = $question_row[qnumber]";
	if($getv_select_item != 0) //항목 조건 있는 경우 AND inum = '보고싶은 항목번호'
	{
		$item_query.= " and inum = '$getv_select_item'";	
	}
	
	$item_query_result = mysqli_query($conn, $item_query);
	$sum_count_for_a_item = 0;
	while($item_row = mysqli_fetch_array($item_query_result))
	{
	/*
		$item_row[survey_id]
		$item_row[qnum]
		$item_row[inum]
		$item_row[ititle]
	*/
		$title_for_a_item[$item_num] = $item_row[ititle];
		$count_for_a_item[$item_num] = 0;	//초기화

		$count_for_a_item_woman = 0;		//여성
		$count_for_a_item_man = 0;			//남성

		$count_for_a_item_edulevel1 = 0;	//중졸 이하
		$count_for_a_item_edulevel2 = 0;	//고졸
		$count_for_a_item_edulevel3 = 0;	//대졸
		$count_for_a_item_edulevel4 = 0;	//초대졸
		$count_for_a_item_edulevel5 = 0;	//대학원졸 이상

		$count_for_a_item_age1 = 0;			//10대 이하
		$count_for_a_item_age2 = 0;			//20대
		$count_for_a_item_age3 = 0;			//30대
		$count_for_a_item_age4 = 0;			//40대
		$count_for_a_item_age5 = 0;			//50대 이상

		$result_query = "select * from result where survey_id = '$getv_sid' and question_num = $item_row[qnum] and item_num = $item_row[inum]";
		$result_query_result = mysqli_query($conn, $result_query);
		while($result_row = mysqli_fetch_array($result_query_result))
		{
		/*
			$result_row[ip]
			$result_row[connect_time]
			$result_row[survey_id]
			$result_row[question_num]
			$result_row[item_num]
		*/
			//item_row[qnum]문항의 item_row[inum]항목을 선택한 한사람한사람에 대하여
			//	$getv_select_condition 0:선택안함, 1:남녀, 2:최종학력별, 3:연령대별

			//조건에 상관없이 공통실행
			$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]'".$date_query.$time_query;
			//echo "<p>".$answer_query."</p>";
			$answer_query_result = mysqli_query($conn, $answer_query);
			$num_answer_query_row = mysqli_num_rows($answer_query_result);	//0 또는 1 (아마도)
			$count_for_a_item[$item_num] += $num_answer_query_row;				//기본조건에 만족하는 것의 개수 저장
			if($getv_select_condition == 1)
			{
				//이 항목을 선택한 이 사람이 남성인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and gender = '1'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_man += $num_answer_query_row;

				//이 항목을 선택한 이 사람이 여성인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and gender = '2'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_woman += $num_answer_query_row;
			}
			else if($getv_select_condition == 2)
			{
				//이 항목을 선택한 이 사람이 중졸 이하인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and edulevel = '1'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_edulevel1 += $num_answer_query_row;
				
				//이 항목을 선택한 이 사람이 고졸인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and edulevel = '2'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_edulevel2 += $num_answer_query_row;

				//이 항목을 선택한 이 사람이 초대졸인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and edulevel = '3'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_edulevel3 += $num_answer_query_row;

				//이 항목을 선택한 이 사람이 대졸인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and edulevel = '4'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_edulevel4 += $num_answer_query_row;

				//이 항목을 선택한 이 사람이 대학원졸 이상인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and edulevel = '5'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_edulevel5 += $num_answer_query_row;
			}
			else if($getv_select_condition == 3)
			{
				//이 항목을 선택한 이 사람이 10대 이하인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and age = '1'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_age1 += $num_answer_query_row;				
								
				//이 항목을 선택한 이 사람이 20대인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and age = '2'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_age2 += $num_answer_query_row;				

				//이 항목을 선택한 이 사람이 30대인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and age = '3'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_age3 += $num_answer_query_row;				

				//이 항목을 선택한 이 사람이 40대인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and age = '4'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_age4 += $num_answer_query_row;				

				//이 항목을 선택한 이 사람이 50대 이상인지
				$answer_query = "select * from answer where ip = '$result_row[ip]' and connect_time = '$result_row[connect_time]' and survey_id = '$result_row[survey_id]' and age = '5'".$date_query.$time_query;
				$answer_query_result = mysqli_query($conn, $answer_query);
				$num_answer_query_row = mysqli_num_rows($answer_query_result);
				$count_for_a_item_age5 += $num_answer_query_row;				
			}
		}
		$sum_count_for_a_item += $count_for_a_item[$item_num];

		//1개 항목에 대한 집계됐음(루프 돌면서 반복 수행)
		if($getv_select_condition != 0)	//항목 번호와 
		{
			if($getv_select_item == 0)
			{
				$item_num++;
				echo "<font style='font-size:12px'>";
				if($item_num == 1) echo "① ";
				else if($item_num == 2) echo "② ";
				else if($item_num == 3) echo "③ ";
				else if($item_num == 4) echo "④ ";
				else if($item_num == 5) echo "⑤ ";
				else if($item_num == 6) echo "⑥ ";
				else if($item_num == 7) echo "⑦ ";
				else if($item_num == 8) echo "⑧ ";
				else if($item_num == 9) echo "⑨ ";
				else echo "$item_num ";
			
				echo "</font">
				
				$item_num--;
				echo "<font style='font-size:13px;font-weight:bold'>$title_for_a_item[$item_num]</font><br>";
			}
		}

		if($getv_select_item != 0)
		{
			echo "선택 항목 : <b>".$item_row[ititle];
			echo "</b><br />\n";
		}

		//여기서 작은모양의 그래프 출력가능
		if($getv_select_condition == 1)	//0일때는 항목(item)별로 그래프를 출력하지 않으므로 여기서 처리 안함.
		{
			if($count_for_a_item_man+$count_for_a_item_woman == 0)
			{
				echo "&nbsp;&nbsp;이 항목에 응답자가 없습니다.";
			}
			else
			{
				$percent_man = $count_for_a_item_man/($count_for_a_item_man+$count_for_a_item_woman)*100;
				$percent_woman = $count_for_a_item_woman/($count_for_a_item_man+$count_for_a_item_woman)*100;

				if($count_for_a_item_woman == 0) $percent_woman = 1;
					
				//이 항목에 대해 'chl=남|여' 로 작은 그래프 출력
				echo "<br><img src=http://chart.apis.google.com/chart?cht=p3&chco=55F640,FF0000&chs=500x100&chl=남(";
				echo $count_for_a_item_man;
				echo "명)|여(";
				echo $count_for_a_item_woman;
				echo "명)&chd=t:$percent_man,$percent_woman>";
			}
			
			echo "<br /><br />";
		}
		else if($getv_select_condition == 2)
		{
			if($count_for_a_item_edulevel1+$count_for_a_item_edulevel2+$count_for_a_item_edulevel3+$count_for_a_item_edulevel4+$count_for_a_item_edulevel5 == 0)
			{
				echo "&nbsp;&nbsp;이 항목에 응답자가 없습니다.";
			}
			else
			{
				$percent_edulevel1 = $count_for_a_item_edulevel1/($count_for_a_item_edulevel1+$count_for_a_item_edulevel2+$count_for_a_item_edulevel3+$count_for_a_item_edulevel4+$count_for_a_item_edulevel5)*100;
				$percent_edulevel2 = $count_for_a_item_edulevel2/($count_for_a_item_edulevel1+$count_for_a_item_edulevel2+$count_for_a_item_edulevel3+$count_for_a_item_edulevel4+$count_for_a_item_edulevel5)*100;
				$percent_edulevel3 = $count_for_a_item_edulevel3/($count_for_a_item_edulevel1+$count_for_a_item_edulevel2+$count_for_a_item_edulevel3+$count_for_a_item_edulevel4+$count_for_a_item_edulevel5)*100;
				$percent_edulevel4 = $count_for_a_item_edulevel4/($count_for_a_item_edulevel1+$count_for_a_item_edulevel2+$count_for_a_item_edulevel3+$count_for_a_item_edulevel4+$count_for_a_item_edulevel5)*100;
				$percent_edulevel5 = $count_for_a_item_edulevel5/($count_for_a_item_edulevel1+$count_for_a_item_edulevel2+$count_for_a_item_edulevel3+$count_for_a_item_edulevel4+$count_for_a_item_edulevel5)*100;

				if($count_for_a_item_edulevel1 == 0) $percent_edulevel1 = 1;
				if($count_for_a_item_edulevel2 == 0) $percent_edulevel2 = 1;
				if($count_for_a_item_edulevel3 == 0) $percent_edulevel3 = 1;
				if($count_for_a_item_edulevel4 == 0) $percent_edulevel4 = 1;
				if($count_for_a_item_edulevel5 == 0) $percent_edulevel5 = 1;
				
				//이 항목에 대해 'chl=중졸 이하|고졸|초대졸|대졸|대학원졸 이상' 로 작은 그래프 출력
				echo "<br><img src=http://chart.apis.google.com/chart?cht=p3&chco=55F640,FF0000&chs=500x100&chl=중졸이하(";
				echo $count_for_a_item_edulevel1;
				echo "명)|고졸(";
				echo $count_for_a_item_edulevel2;
				echo "명)|초대졸(";
				echo $count_for_a_item_edulevel3;
				echo "명)|대졸(";
				echo $count_for_a_item_edulevel4;
				echo "명)|대학원졸이상(";
				echo $count_for_a_item_edulevel5;
				echo "명)&chd=t:$percent_edulevel1,$percent_edulevel2,$percent_edulevel3,$percent_edulevel4,$percent_edulevel5>";
			}
			
			echo "<br /><br />";
		}
		else if($getv_select_condition == 3)
		{
			$count_sum = $count_for_a_item_age1+$count_for_a_item_age2+$count_for_a_item_age3+$count_for_a_item_age4+$count_for_a_item_age5;
			if($count_sum == 0)
			{
				echo "&nbsp;&nbsp;이 항목에 응답자가 없습니다.";
			}
			else
			{
				$percent_age1 = $count_for_a_item_age1/($count_sum)*100;
				$percent_age2 = $count_for_a_item_age2/($count_sum)*100;
				$percent_age3 = $count_for_a_item_age3/($count_sum)*100;
				$percent_age4 = $count_for_a_item_age4/($count_sum)*100;
				$percent_age5 = $count_for_a_item_age5/($count_sum)*100;

				if($count_for_a_item_age1 == 0) $percent_age1 = 1;
				if($count_for_a_item_age2 == 0) $percent_age2 = 1;
				if($count_for_a_item_age3 == 0) $percent_age3 = 1;
				if($count_for_a_item_age4 == 0) $percent_age4 = 1;
				if($count_for_a_item_age5 == 0) $percent_age5 = 1;
				
				//이 항목에 대해 'ch1=10대 이하|20대|30대|40대|50대 이상' 로 작은 그래프 출력
				echo "<br><img src=http://chart.apis.google.com/chart?cht=p3&chco=55F640,FF0000&chs=500x100&chl=10대이하(";
				echo $count_for_a_item_age1;
				echo "명)|20대(";
				echo $count_for_a_item_age2;
				echo "명)|30대(";
				echo $count_for_a_item_age3;
				echo "명)|40대(";
				echo $count_for_a_item_age4;
				echo "명)|50대이상(";
				echo $count_for_a_item_age5;
				echo "명)&chd=t:$percent_age1,$percent_age2,$percent_age3,$percent_age4,$percent_age5>";
			}
			
			echo "<br /><br />";		
		}

		$item_num++;
	}
	if($getv_select_condition != 0) echo "<hr noshade>";
	//1개 문항에 대한 집계됐음(루프 돌면서 반복 수행)
	//이 문항에는 $item_num 만큼의 항목이 있고
	//$title_for_a_item[]에 항목이름(순서대로)
	//$count_for_a_item[]에 카운트가 들어있다.
	
	if($getv_select_condition == 0)
	{
		//echo "총 $sum_count_for_a_item 개의 응답<br>";
		
		if($sum_count_for_a_item == 0)
		{
			echo "&nbsp;&nbsp;이 문항에 응답자가 없습니다.<br>";
		}
		else
		{
			//여기서 큰모양의 그래프 출력가능
			$valid_item_title = range(0,0);
			$valid_item_count = range(0,0);

			
			for($i=0,$n=0;$i<$item_num;$i++)
			{
				if($count_for_a_item[$i] != 0)
				{
					$valid_item_title[$n] = $title_for_a_item[$i]."(".$count_for_a_item[$i]."명)";
					$valid_item_count[$n++] = round($count_for_a_item[$i]/$sum_count_for_a_item*100,1);
				}
			}		
			
			for($i=0;$i<$item_num;$i++)
			{
				if($count_for_a_item[$i] == 0)
				{
					$valid_item_title[$n] = $title_for_a_item[$i]."(".$count_for_a_item[$i]."명)";
					$valid_item_count[$n++] = 1;
				}
			}

			echo "<center><img src=http://chart.apis.google.com/chart?cht=p3&chco=55F640,FF0000&chs=550x150&chl=";
			echo urlencode(implode("|", $valid_item_title));
			echo "&chd=t:";
			echo urlencode(implode(",", $valid_item_count));
			echo "></center><br><br><hr noshade>";
		}
	}
}



//1개 설문에 대한 통계끝

/*
	$getv_sid = $_GET["sid"];
	$getv_select_question = $_GET["select_question"];
	$getv_select_item = $_GET["select_item"];
	$getv_select_condition = $_GET["select_condition"];
	$getv_select_date = $_GET["select_date"];
	$getv_lower_time = $_GET["lower_time"];
	$getv_upper_time = $_GET["upper_time"];
*/



?>
</div>


</div>





<iframe style="display:none" src="refresh.php"></iframe>
<iframe style="display:none" src="monitor.php"></iframe>
<div id="footer">
</div>
</div>
</body>
<script language="JavaScript">
Array.prototype.get = function(index)
{ 
	return this[index]; 
}

Array.prototype.set = function(object)
{ 
	this[this.length] = object; 
}

Coditech = new Object();
Coditech.Version="1.0.0.0";
Coditech.Framework = new Object();

Coditech.Framework.Category = function(name,value)
{
	var _name = name;
	var _value = value;
	var _subcategory = new Array();

	this.getName = function(){return _name;}
	this.getValue = function(){ return _value;}
	this.add = function(name,value)
	{
		_subcategory.set(new Coditech.Framework.Category(name,value)); 
	}

	this.get = function(index)
	{
		return _subcategory.get(index);
	}
	this.length = function(){ return _subcategory.length;}
}

function addCategory(category,name,value)
{
	category.add(name,value);
}
ROOT_CATEGORY_NODE = new Coditech.Framework.Category("ROOT","");
addCategory(ROOT_CATEGORY_NODE,"0","모든 문항");
addCategory(ROOT_CATEGORY_NODE.get(0),"0","모든 항목");
<?
//$getv_sid 에 해당되는 모든 문항(qnum : 1부터 순차적 카운트)
//각 문항에 대한 항목들 추가

$result_question = mysqli_query($conn, "select * from question where survey_id = '$getv_sid'");

$question_n = 0;
while($question_row = mysqli_fetch_array($result_question))
{
	//$question_row[survey_id]
	//$question_row[qnumber]
	//$question_row[qtitle]
	//$question_row[is_multi]

	$question_n++;
	$result_item = mysqli_query($conn, "select * from item where survey_id = '$getv_sid' and qnum = '$question_row[qnumber]'"); 
	echo "addCategory(ROOT_CATEGORY_NODE,\"$question_row[qnumber]\",\"$question_row[qtitle]\");\n";
	$item_n = 0;

	echo "addCategory(ROOT_CATEGORY_NODE.get($question_n),\"0\",\"모든 항목\");\n";
	while($item_row = mysqli_fetch_array($result_item))
	{
		//$item_row[survey_id]
		//$item_row[qnum]
		//$item_row[inum]
		//$item_row[ititle]
		$item_n++;
		echo "addCategory(ROOT_CATEGORY_NODE.get($question_n),\"$item_row[inum]\",\"$item_row[ititle]\");\n";
	}
	
}
	
?>

/*
addCategory(ROOT_CATEGORY_NODE,"CATE_1","흰색");
addCategory(ROOT_CATEGORY_NODE.get(1),"CATE_1_0","-사이즈 선택-");
addCategory(ROOT_CATEGORY_NODE.get(1),"CATE_1_1","44");
addCategory(ROOT_CATEGORY_NODE.get(1),"CATE_1_2","55");

addCategory(ROOT_CATEGORY_NODE,"CATE_2","검정");
addCategory(ROOT_CATEGORY_NODE.get(2),"CATE_2_0","-사이즈 선택-");
addCategory(ROOT_CATEGORY_NODE.get(2),"CATE_2_2","55");
addCategory(ROOT_CATEGORY_NODE.get(2),"CATE_2_3","66");

addCategory(ROOT_CATEGORY_NODE,"CATE_3","카키");
addCategory(ROOT_CATEGORY_NODE.get(3),"CATE_3_0","-사이즈 선택-");
addCategory(ROOT_CATEGORY_NODE.get(3),"CATE_3_1","44");

addCategory(ROOT_CATEGORY_NODE,"CATE_4","보라");
addCategory(ROOT_CATEGORY_NODE.get(4),"CATE_4_0","-사이즈 선택-");
addCategory(ROOT_CATEGORY_NODE.get(4),"CATE_4_3","66");
*/
function initialize()
{
	var defaultForm = document.getElementById('viewresult_form');
	if (null != defaultForm)
	{
		createOption(defaultForm.select_question, ROOT_CATEGORY_NODE);
		createOption(defaultForm.select_item, ROOT_CATEGORY_NODE.get(0));
		createOption(defaultForm.test, ROOT_CATEGORY_NODE.get(0).get(0));
	}
}

function clearSelectBox(selectBox)
{
	if (null == selectBox || null == selectBox.options){
		return;
	}
	var length = selectBox.options.length;
	for (var index=0;index<length ;index++){ selectBox.options.remove(0); }
}

function createOption(selectBox,category)
{
	if (null == category) return;

	clearSelectBox(selectBox);

	for (var index=0;index<category.length(); index++)
	{
		var c = category.get(index);
		var option = new Option();

		option.value = c.getName();
		option.text = c.getValue();

		selectBox.options.add(option);
	}
}

function changeSubject(form)
{
	var category = ROOT_CATEGORY_NODE.get(form.select_question.selectedIndex);
	if (null != category)
	{
		createOption(form.select_item,category);
		createOption(form.test,category.get(0));
	}
}

function changeContents(form)
{
	var category = null;

	category =ROOT_CATEGORY_NODE.get(form.select_question.selectedIndex); 
	category = category.get(form.select_item.selectedIndex);

	if (null != category){
		createOption(form.test,category);
	}
}

window.onload = initialize;
</script>
</html>

<?php
mysqli_close($conn);
//require_once("footer.php");
?>